package com.fankai.lease.web.admin.controller.apartment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fankai.lease.common.result.Result;
import com.fankai.lease.model.entity.AttrKey;
import com.fankai.lease.model.entity.AttrValue;
import com.fankai.lease.web.admin.service.AttrKeyService;
import com.fankai.lease.web.admin.service.AttrValueService;
import com.fankai.lease.web.admin.vo.attr.AttrKeyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "房间属性管理")
@RestController
@AllArgsConstructor
@RequestMapping("/admin/attr")
public class AttrController {
    private AttrValueService attrValueService;
    private AttrKeyService attrKeyService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        boolean b = attrKeyService.saveOrUpdate(attrKey);
        return Result.ok(b);
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        boolean b = attrValueService.saveOrUpdate(attrValue);
        return Result.ok(b);
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        ArrayList<AttrKeyVo> voArrayList = new ArrayList<>();
        AttrKeyVo attrKeyVo;
        for (AttrKey attrKey : attrKeyService.list()) {
            attrKeyVo = new AttrKeyVo();
            BeanUtils.copyProperties(attrKey, attrKeyVo);
            LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AttrValue::getAttrKeyId, attrKey.getId())
                    .eq(AttrValue::getIsDeleted, 0);
            List<AttrValue> attrValueList = attrValueService.list(queryWrapper);
            attrKeyVo.setAttrValueList(attrValueList);
            voArrayList.add(attrKeyVo);
        }
        return Result.ok(voArrayList);
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {
        attrKeyService.removeById(attrKeyId);
        LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrValue::getAttrKeyId, attrKeyId);
        attrValueService.remove(queryWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        attrValueService.removeById(id);
        return Result.ok();
    }

}
